package com.qfjy.homework.wangjiahui;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**获取代理对象工具类
 * @author : playmaker
 * @version : 1.0
 * @date : 2022/11/10 19:09
 */
public class ProxyUtils {
    //目标代理对象
    private Object target;

    public ProxyUtils(Object target){
        this.target=target;
    }


    //获取代理对象
    public Object getProxy(){
        //获取目标代理对象的类加载器
        ClassLoader classLoader = target.getClass().getClassLoader();
        //目标代理对象的实现接口
        Class<?>[] interfaces = target.getClass().getInterfaces();
        //调用重写的方法
        InvocationHandler invocationHandler = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                return method.invoke(target,args);
            }
        };
        return Proxy.newProxyInstance(classLoader,interfaces,invocationHandler);
    }
}
